{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "view-in-github",
    "colab_type": "text"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/alexfazio/crewAI-quickstart/blob/main/crewai-example-task-event-planning-quickstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "By9Je_QqSUGw"
   },
   "source": "# Automate Event Planning"
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "tfTPMCRgSUGy"
   },
   "source": [
    "If you're running this notebook on your own machine, you can install the following:\n",
    "```Python\n",
    "%pip install crewai==0.28.8 crewai_tools==0.1.6 langchain_community==0.0.29 --quiet\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 64,
    "id": "RbQDt6mJSUGy"
   },
   "outputs": [],
   "source": [
    "# Warning control\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "k12cfmZ8SUGz"
   },
   "source": [
    "- Import libraries, APIs and LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 30,
    "id": "bZnxm_KDSUGz"
   },
   "outputs": [],
   "source": [
    "from crewai import Agent, Crew, Task"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "t7PqG655SUGz"
   },
   "source": [
    "**Note**:\n",
    "- The video uses `gpt-4o`, but due to certain constraints, and in order to offer this course for free to everyone, the code you'll run here will use `gpt-3.5-turbo`.\n",
    "- You can use `gpt-4o` when you run the notebook _locally_ (using `gpt-4o` will not work on the platform)\n",
    "- Thank you for your understanding!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 115,
    "id": "AiQCQ2CLSUGz"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from utils import get_openai_api_key,get_serper_api_key\n",
    "\n",
    "openai_api_key = get_openai_api_key()\n",
    "os.environ[\"OPENAI_MODEL_NAME\"] = 'gpt-3.5-turbo'\n",
    "os.environ[\"SERPER_API_KEY\"] = get_serper_api_key()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "j8Q4BDiPSUGz"
   },
   "source": [
    "## crewAI Tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 98,
    "id": "-MXCboJsSUGz"
   },
   "outputs": [],
   "source": [
    "from crewai_tools import ScrapeWebsiteTool, SerperDevTool\n",
    "\n",
    "# Initialize the tools\n",
    "search_tool = SerperDevTool()\n",
    "scrape_tool = ScrapeWebsiteTool()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XEXovfTBSUGz"
   },
   "source": [
    "## Creating Agents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 268,
    "id": "NHCoC__NSUGz"
   },
   "outputs": [],
   "source": [
    "# Agent 1: Venue Coordinator\n",
    "venue_coordinator = Agent(\n",
    "    role=\"Venue Coordinator\",\n",
    "    goal=\"Identify and book an appropriate venue \"\n",
    "    \"based on event requirements\",\n",
    "    tools=[search_tool, scrape_tool],\n",
    "    verbose=True,\n",
    "    backstory=(\n",
    "        \"With a keen sense of space and \"\n",
    "        \"understanding of event logistics, \"\n",
    "        \"you excel at finding and securing \"\n",
    "        \"the perfect venue that fits the event's theme, \"\n",
    "        \"size, and budget constraints.\"\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 285,
    "id": "B33o3zrvSUGz"
   },
   "outputs": [],
   "source": [
    " # Agent 2: Logistics Manager\n",
    "logistics_manager = Agent(\n",
    "    role='Logistics Manager',\n",
    "    goal=(\n",
    "        \"Manage all logistics for the event \"\n",
    "        \"including catering and equipmen\"\n",
    "    ),\n",
    "    tools=[search_tool, scrape_tool],\n",
    "    verbose=True,\n",
    "    backstory=(\n",
    "        \"Organized and detail-oriented, \"\n",
    "        \"you ensure that every logistical aspect of the event \"\n",
    "        \"from catering to equipment setup \"\n",
    "        \"is flawlessly executed to create a seamless experience.\"\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 251,
    "id": "NIAqkrlLSUG0"
   },
   "outputs": [],
   "source": [
    "# Agent 3: Marketing and Communications Agent\n",
    "marketing_communications_agent = Agent(\n",
    "    role=\"Marketing and Communications Agent\",\n",
    "    goal=\"Effectively market the event and \"\n",
    "         \"communicate with participants\",\n",
    "    tools=[search_tool, scrape_tool],\n",
    "    verbose=True,\n",
    "    backstory=(\n",
    "        \"Creative and communicative, \"\n",
    "        \"you craft compelling messages and \"\n",
    "        \"engage with potential attendees \"\n",
    "        \"to maximize event exposure and participation.\"\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "v_OqO_WJSUG0"
   },
   "source": [
    "## Creating Venue Pydantic Object\n",
    "\n",
    "- Create a class `VenueDetails` using [Pydantic BaseModel](https://docs.pydantic.dev/latest/api/base_model/).\n",
    "- Agents will populate this object with information about different venues by creating different instances of it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 149,
    "id": "F9F_1rs0SUG0"
   },
   "outputs": [],
   "source": [
    "from pydantic import BaseModel\n",
    "# Define a Pydantic model for venue details\n",
    "# (demonstrating Output as Pydantic)\n",
    "class VenueDetails(BaseModel):\n",
    "    name: str\n",
    "    address: str\n",
    "    capacity: int\n",
    "    booking_status: str"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LtO5v9bRSUG0"
   },
   "source": [
    "## Creating Tasks\n",
    "- By using `output_json`, you can specify the structure of the output you want.\n",
    "- By using `output_file`, you can get your output in a file.\n",
    "- By setting `human_input=True`, the task will ask for human feedback (whether you like the results or not) before finalising it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 200,
    "id": "LrMzfUxPSUG0"
   },
   "outputs": [],
   "source": [
    "venue_task = Task(\n",
    "    description=\"Find a venue in {event_city} \"\n",
    "                \"that meets criteria for {event_topic}.\",\n",
    "    expected_output=\"All the details of a specifically chosen\"\n",
    "                    \"venue you found to accommodate the event.\",\n",
    "    human_input=True,\n",
    "    output_json=VenueDetails,\n",
    "    output_file=\"venue_details.json\",\n",
    "      # Outputs the venue details as a JSON file\n",
    "    agent=venue_coordinator\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Jf5INrwOSUG0"
   },
   "source": [
    "- By setting `async_execution=True`, it means the task can run in parallel with the tasks which come after it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 200,
    "id": "Rbx_gNjGSUG0"
   },
   "outputs": [],
   "source": [
    "logistics_task = Task(\n",
    "    description=\"Coordinate catering and \"\n",
    "                 \"equipment for an event \"\n",
    "                 \"with {expected_participants} participants \"\n",
    "                 \"on {tentative_date}.\",\n",
    "    expected_output=\"Confirmation of all logistics arrangements \"\n",
    "                    \"including catering and equipment setup.\",\n",
    "    human_input=True,\n",
    "    async_execution=True,\n",
    "    agent=logistics_manager\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 183,
    "id": "vuucUfPRSUG0"
   },
   "outputs": [],
   "source": [
    "marketing_task = Task(\n",
    "    description=\"Promote the {event_topic} \"\n",
    "                \"aiming to engage at least\"\n",
    "                \"{expected_participants} potential attendees.\",\n",
    "    expected_output=\"Report on marketing activities \"\n",
    "                    \"and attendee engagement formatted as markdown.\",\n",
    "    async_execution=True,\n",
    "    output_file=\"marketing_report.md\",  # Outputs the report as a text file\n",
    "    agent=marketing_communications_agent\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "zHOl9_9cSUG0"
   },
   "source": [
    "## Creating the Crew"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Sml-3Tc8SUG0"
   },
   "source": [
    "**Note**: Since you set `async_execution=True` for `logistics_task` and `marketing_task` tasks, now the order for them does not matter in the `tasks` list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 217,
    "id": "p8Mq2ar3SUG0"
   },
   "outputs": [],
   "source": [
    "# Define the crew with agents and tasks\n",
    "event_management_crew = Crew(\n",
    "    agents=[venue_coordinator,\n",
    "            logistics_manager,\n",
    "            marketing_communications_agent],\n",
    "\n",
    "    tasks=[venue_task,\n",
    "           logistics_task,\n",
    "           marketing_task],\n",
    "\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "hnUgBgWlSUG1"
   },
   "source": [
    "## Running the Crew\n",
    "\n",
    "- Set the inputs for the execution of the crew."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 200,
    "id": "pqYXRHnaSUG1"
   },
   "outputs": [],
   "source": [
    "event_details = {\n",
    "    'event_topic': \"Tech Innovation Conference\",\n",
    "    'event_description': \"A gathering of tech innovators \"\n",
    "                         \"and industry leaders \"\n",
    "                         \"to explore future technologies.\",\n",
    "    'event_city': \"San Francisco\",\n",
    "    'tentative_date': \"2024-09-15\",\n",
    "    'expected_participants': 500,\n",
    "    'budget': 20000,\n",
    "    'venue_type': \"Conference Hall\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7Qr0RRkFSUG1"
   },
   "source": [
    "**Note 1**: LLMs can provide different outputs for they same input, so what you get might be different than what you see in the video."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "LkH5JmNASUG1"
   },
   "source": [
    "**Note 2**:\n",
    "- Since you set `human_input=True` for some tasks, the execution will ask for your input before it finishes running.\n",
    "- When it asks for feedback, use your mouse pointer to first click in the text box before typing anything."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 30,
    "id": "X0JoI32bSUG1"
   },
   "outputs": [],
   "source": [
    "result = event_management_crew.kickoff(inputs=event_details)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "b8qZtTQISUG1"
   },
   "source": [
    "- Display the generated `venue_details.json` file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 132,
    "id": "bAteuAVqSUG1"
   },
   "outputs": [],
   "source": [
    "import json\n",
    "from pprint import pprint\n",
    "\n",
    "with open('venue_details.json') as f:\n",
    "   data = json.load(f)\n",
    "\n",
    "pprint(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XZibXQP6SUG1"
   },
   "source": [
    "- Display the generated `marketing_report.md` file.\n",
    "\n",
    "**Note**: After `kickoff` execution has successfully ran, wait an extra 45 seconds for the `marketing_report.md` file to be generated. If you try to run the code below before the file has been generated, your output would look like:\n",
    "\n",
    "```\n",
    "marketing_report.md\n",
    "```\n",
    "\n",
    "If you see this output, wait some more and than try again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "height": 47,
    "id": "Laq81QzTSUG1"
   },
   "outputs": [],
   "source": [
    "# @title 🖥️ Display the results of your crew as markdown\n",
    "from IPython.display import display, Markdown\n",
    "\n",
    "markdown_text = result.raw  # Adjust this based on the actual attribute\n",
    "\n",
    "# Display the markdown content\n",
    "display(Markdown(markdown_text))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "colab": {
   "provenance": [],
   "include_colab_link": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
